package com.macro.mall.controller;


import com.macro.mall.annotationUtil.CurrentUserName;
import com.macro.mall.common.api.CommonPage;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.dto.FilterParam;
import com.macro.mall.dto.UmsAdminDTO;
import com.macro.mall.dto.UmsPlantingTechnologyDto;
import com.macro.mall.model.UmsPlantingTechnologyType;
import com.macro.mall.service.UmsAdminService;
import com.macro.mall.service.UmsPlantingTechnologyService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 种植技术controller
 */
@Controller
@RequestMapping("/planting")
public class UmsPlantingTechnologyController {
    Logger logger = LoggerFactory.getLogger(UmsPlantingTechnologyController.class);
    @Autowired
    private UmsPlantingTechnologyService plantingService;

    @Autowired
    private UmsAdminService umsAdminService;


    /**
     * 后台添加种植技术分类
     *
     * @param umsPlantingTechnologyType
     * @return
     * @author 高强
     * @Date 2020年9月1日
     */
    @RequestMapping(value = "/createType", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult createType(@RequestBody UmsPlantingTechnologyType umsPlantingTechnologyType) {
        int count = plantingService.creatType(umsPlantingTechnologyType);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    /**
     * 后台删除种植技术分类
     * @param id
     * @return
     * @author 高强
     * @Date 2020年9月1日
     */
    @RequestMapping(value = "/deleteType", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult deleteType(@RequestParam Long id) {
        int count = plantingService.deleteType(id);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    /**
     * 查询所有文章分类
     * @param storeId
     * @param pageSize
     * @param pageNum
     * @return
     */
    @RequestMapping(value = "getTypeList", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<CommonPage<UmsPlantingTechnologyType>> getPlantingTechnologyType(@RequestBody FilterParam param){
        logger.info("查询所有文章分类---------------------"+param.getType());
        List<UmsPlantingTechnologyType> typeList = plantingService.getPlantingTechnologyType(param.getStoreId(), param.getKeyWord(),
                param.getPageSize(), param.getPageNum());
        return CommonResult.success(CommonPage.restPage(typeList));
    }

    /**
     * 根据id获取文章分类
     * @param id
     * @return
     */
    @RequestMapping(value = "getType", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<UmsPlantingTechnologyType> getType(@RequestParam Long id){
        UmsPlantingTechnologyType type = plantingService.getType(id);
        return CommonResult.success(type);
    }

    /**
     * 新增种植技术
     *
     * @param umsPlantingTechnologyDto 种植技术bean
     * @param typeId                种植技术分类id
//     * @param pic                   图片url
//     * @param videoId               视频标识id
     * @return
     * @author 高强
     * @Date 2020年9月1日
     */
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult createPlantingTechnology(@CurrentUserName String currentUserName, @RequestBody UmsPlantingTechnologyDto umsPlantingTechnologyDto
                                                 ) {
        UmsAdminDTO umsAdminDTO =  umsAdminService.getAdminDTOByUserName(currentUserName);
        int count = plantingService.creat(umsPlantingTechnologyDto,umsAdminDTO);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    /**
     * 修改文章分类
     * @param id
     * @param umsPlantingTechnologyType
     * @return
     */
    @RequestMapping(value = "/updateType", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult updateType( @RequestBody UmsPlantingTechnologyType umsPlantingTechnologyType){
        int count = plantingService.updateType(umsPlantingTechnologyType);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    /**
     * 后台显示当前门店种植技术
     *
     * @param storeId 门店id
     * @param keyWord 门店标题模糊查询
     * @return
     */
    @RequestMapping(value = "/getPlant", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public CommonResult<CommonPage<UmsPlantingTechnologyDto>> getPlantList(@CurrentUserName String currentUserName,@RequestBody FilterParam param) {
        UmsAdminDTO umsAdminDTO =  umsAdminService.getAdminDTOByUserName(currentUserName);
        CommonPage<UmsPlantingTechnologyDto> plantList = plantingService.getPlantList(umsAdminDTO.getStoreId(), param.getKeyWord(),
                param.getPageNum(), param.getPageSize());
        return CommonResult.success(plantList);
    }


    /**
     * 修改当前种植技术
     * @param id
     * @param umsPlantingTechnology
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult updatePlant(@RequestBody UmsPlantingTechnologyDto umsPlantingTechnologyDto) {
        return plantingService.updatePlant(umsPlantingTechnologyDto);
    }

    /**
     * 删除单条种植技术
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult deletePlant(@RequestParam(value = "id") Long id){
        return plantingService.deletePlant(id);
    }

    /**
     * 批量删除种植技术
     * @param ids
     * @return
     */
    @RequestMapping(value = "/deleteList", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult deletePlantList(@RequestParam(value = "ids") List<Long> ids){
        return plantingService.deletePlantList(ids);
    }
}
